package com.ws.shop.action.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ws.shop.entity.AdminUser;

import net.sf.json.JSONObject;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;

public class BaseFrontAction {
	private static final long serialVersionUID = 6718838822334455667L;

	public static final String VIEW = "view";
	public static final String LIST = "list";
	public static final String STATUS = "status";
	public static final String WARN = "warn";
	public static final String SUCCESS = "success";
	public static final String ERROR = "error";
	public static final String MESSAGE = "message";
	public static final String CONTENT = "content";
	public static final String LOGIN_URL = "/admin/admin!login.action";
	public static final String REDIRECT_URL = "redirectionUrl";
	public final static String LOGIN_MEMBER_SESSION_KEY = "loginMember";
	
	protected String logInfo;// 日志记录信息
	protected String redirectionUrl;// 操作提示后的跳转URL,为null则返回前一页
	
	public String input() {
		return null;
	}


	// AJAX输出，返回null
	public String ajax(HttpServletResponse response, String content, String type) {
		try {
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// AJAX输出文本，返回null
	public String ajaxText(HttpServletResponse response,String text) {
		return ajax(response, text, "text/plain");
	}

	// AJAX输出HTML，返回null
	public String ajaxHtml(HttpServletResponse response,String html) {
		return ajax(response,html, "text/html");
	}

	// AJAX输出XML，返回null
	public String ajaxXml(HttpServletResponse response,String xml) {
		return ajax(response,xml, "text/xml");
	}

	// 根据字符串输出JSON，返回null
	public String ajaxJson(HttpServletResponse response,String jsonString) {
		return ajax(response,jsonString, "text/html");
	}
	
	// 根据Map输出JSON，返回null
	public String ajaxJson(HttpServletResponse response,List jsonList) {
		JSONObject jsonObject = JSONObject.fromObject(jsonList);
		return ajax(response,jsonObject.toString(), "text/html");
	}
	
	// 根据Map输出JSON，返回null
	public String ajaxJson(HttpServletResponse response,Map jsonMap) {
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(response,jsonObject.toString(), "text/html");
	}
	
	// 输出JSON警告消息，返回null
	public String ajaxJsonWarnMessage(HttpServletResponse response,String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, WARN);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(response,jsonObject.toString(), "text/html");
	}
	
	// 输出JSON成功消息，返回null
	public String ajaxJsonSuccessMessage(HttpServletResponse response,String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(response,jsonObject.toString(), "text/html");
	}
	
	// 输出JSON错误消息，返回null
	public String ajaxJsonErrorMessage(HttpServletResponse response,String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, ERROR);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(response,jsonObject.toString(), "text/html");
	}
	
	// 设置页面不缓存
	public void setResponseNoCache(HttpServletResponse response) {
		response.setHeader("progma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
	}
	
	public TemplateHashModel getStatics() {  
		return BeansWrapper.getDefaultInstance().getStaticModels();  
	} 
    
	public HttpSession getSession(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		return session;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String,Object> getLoginMember(HttpSession session){
		if(session == null)
			return null;
		return (Map)session.getAttribute(LOGIN_MEMBER_SESSION_KEY);
	}
}
